package com.wf.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wf.domain.SysPermission;
import com.wf.domain.SysRole;
import com.wf.domain.SysRolePermission;
import com.wf.dto.AssignPermDTO;
import com.wf.mapper.SysRolePermissionMapper;
import com.wf.mapper.SysUserMapper;
import com.wf.service.SysPermissionService;
import com.wf.mapper.SysPermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author mpk16
 * @description 针对表【sys_permission(权限表)】的数据库操作Service实现
 * @createDate 2024-10-02 09:54:26
 */
@Service
@Slf4j
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission>
        implements SysPermissionService {

    @Autowired
    SysUserMapper sysUserMapper;


    @Autowired
    SysPermissionMapper sysPermissionMapper;

    @Autowired
    SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Tree<Integer>> getPermissionList(String roleId) {

        List<SysPermission> permissions = sysUserMapper.getPermissionByRoleId(roleId);
        List<SysPermission> sysPermissions = sysPermissionMapper.selectList(null);
        List<TreeNode<Integer>> nodeList = CollUtil.newArrayList();
        sysPermissions.forEach(
                permission -> {
                    TreeNode<Integer> treeNode = new TreeNode<>(permission.getId(), permission.getParentId(), permission.getPermissionName(), permission.getSort());
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("ismenu", permission.getIsMenu());
                    map.put("permissionId",permission.getPermissionId());
                    if (permission.getIsMenu() == 1) {
                        map.put("url", permission.getUrl());
                        map.put("icon", permission.getIcon());

                    } else {
                        map.put("perm", permission.getPerm());

                    }

                    if (permissions.contains(permission)
                    ) {
                        map.put("checked", true);
                    } else {
                        map.put("checked", false);
                    }
                    treeNode.setExtra(map);
                    nodeList.add(treeNode);
                }
        );
        // 0表示最顶层的id是0
        return TreeUtil.build(nodeList, 0);
    }

    @Override
    @Transactional
    public void updatePermission(AssignPermDTO assignPermDTO) {

        LambdaQueryWrapper<SysRolePermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRolePermission::getRoleId,assignPermDTO.getId());
        sysRolePermissionMapper.delete(lambdaQueryWrapper);
        log.info("updatePermission:{}", assignPermDTO);

        if (ObjectUtil.isNotEmpty(assignPermDTO.getPermission())
        ) {
            for (int i = 0; i < assignPermDTO.getPermission().size(); i++) {

                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setRoleId(assignPermDTO.getId());
                sysRolePermission.setPermissionId(assignPermDTO.getPermission().get(i));
                sysRolePermissionMapper.insert(sysRolePermission);
            }

        }

    }

    @Override
    @Transactional(readOnly = true)
    public List<Tree<Integer>> getMenus(List<SysRole> roles) {
        List<SysPermission> permissions = new ArrayList<>();
        roles.forEach(
                role -> {
                    permissions.addAll(sysUserMapper.getPermissionByRoleId(role.getRoleId()));
                }
        );
        List<TreeNode<Integer>> nodeList = CollUtil.newArrayList();
        permissions.forEach(
                permission -> {
                    if (permission.getIsMenu() == 1) {
                        TreeNode<Integer> treeNode = new TreeNode<>(permission.getId(), permission.getParentId(), permission.getPermissionName(), permission.getSort());
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("url", permission.getUrl());
                        map.put("icon", permission.getIcon());
                        treeNode.setExtra(map);
                        nodeList.add(treeNode);
                    }

                }
        );
        // 0表示最顶层的id是0
        return TreeUtil.build(nodeList, 0);
    }
}




